package cn.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 线程创建低第三种方式 实现 Callable
 */
public  class MyThreadCallable implements Callable {
    //累加算法
    @Override
    public Object call() throws Exception {
        int sum = 0;
           for (int i=0;i<=20;i++){
               System.out.println(i);
               sum=sum+i;
           }
           return sum;
    }

}
//测试
  class  test{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       MyThreadCallable callable=new MyThreadCallable();
        FutureTask task=new FutureTask(callable);//FutureTask实现接口Runnable
        new Thread(task).start();
        Object sum = task.get();//获取call返回值
        System.out.println("总数:"+sum);

    }
}
